﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace ServiceDeskClient
{
    /// <summary>
    /// Interaction logic for ProgressBar.xaml
    /// </summary>
    public partial class ProgressBar : Window
    {
        public ProgressBar()
        {
            InitializeComponent();
        }

        public static void RunWithProgressBar( Task task)
        {
            bool isShowed = false;
            bool isFinished = false;

            //object sync = new Object();
            var p = new ProgressBar();
            task.ContinueWith(
                        _ =>
                        {
                            p.Dispatcher.SimpleInvoke(
                                ()=>
                                        //lock(sync)
                                        {
                                            isFinished = true;
                                            if (isShowed)
                                                p.Close();
                                        }

                                    );
                        });

            p.Dispatcher.SimpleInvoke(//DispatcherPriority.Normal,
                                                    //new Action(
                                                    () =>
                                                    {
                                                        task.Start();
                                                        //lock(sync)
                                                        {
                                                            if (!isFinished)
                                                            {
                                                                isShowed = true;
                                                                p.ShowDialog();
                                                            }
                                                        }
                                                    }
                                                    );
        }
    }

    
}
